﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WFA_BANHANG.ThuVien;
using System.Data.SqlClient;

namespace WFA_BANHANG.HeThong.DynamicMenu
{
    public partial class frmCreateMenuUser : DevExpress.XtraEditors.XtraForm
    {
        private Boolean bChildTrigger = true;
        private Boolean bParentTrigger = true;

        public frmCreateMenuUser()
        {
            InitializeComponent();
        }

        private void CreateMenuUser_Load(object sender, EventArgs e)
        {
            PopulateTreeview();
        }

        private void PopulateTreeview()
        {
            this.treeView1.Nodes.Clear();
            this.treeView1.CheckBoxes = true;
            MeunLists mls = new MeunLists();
            using (SqlConnection connection = new SqlConnection(@"Server=112.78.6.27; User Id=anhlan_lannh;Password=lannh***14782;Database=anhlan_SaleManager"))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand("usp_GetListMenu", connection))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                    while (reader.Read())
                    {
                        mls.Add(new MeunLists.MenuList()
                        {
                            Depth = int.Parse(reader["Depth"].ToString()),
                            MenuID = int.Parse(reader["MenuID"].ToString()),
                            MenuParent = int.Parse(reader["Parent"].ToString()),
                            MenuName = reader["MenuName"].ToString(),
                        });
                    }
                }
            }

            foreach (MeunLists.MenuList ml in mls)
            {
                MeunLists.MenuList parentMenu = mls.Find(o => o.MenuID == ml.MenuParent);
                if (parentMenu != null)
                {
                    this.treeView1.Nodes.Find(parentMenu.MenuID.ToString(), true)[0].Nodes.Add(ml.MenuID.ToString(), ml.MenuName);
                }
                else
                {
                    this.treeView1.Nodes.Add(ml.MenuID.ToString(), ml.MenuName);
                }
            }

            this.treeView1.ExpandAll();
        }

        void CheckAllChildren(TreeNode tn, Boolean bCheck)
        {
            bParentTrigger = false;
            foreach (TreeNode ctn in tn.Nodes)
            {
                bChildTrigger = false;
                ctn.Checked = bCheck;
                bChildTrigger = true;

                CheckAllChildren(ctn, bCheck);
            }
            bParentTrigger = true;
        }

        void CheckMyParent(TreeNode tn, Boolean bCheck)
        {
            if (tn == null) return;
            if (tn.Parent == null) return;

            bChildTrigger = false;
            bParentTrigger = false;
            tn.Parent.Checked = bCheck;
            CheckMyParent(tn.Parent, bCheck);
            bParentTrigger = true;
            bChildTrigger = true;
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (bChildTrigger)
            {
                CheckAllChildren(e.Node, e.Node.Checked);
            }
            if (bParentTrigger)
            {
                CheckMyParent(e.Node, e.Node.Checked);
            }
        }

    }//the end class
}//the end namespace